package collection;

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * Author itcast
 * Date 2021/10/13 9:02
 * D根据输入的字符串，计算出来每个字符的出现的次数 - treeMap
 * 开发步骤：
 * 1.从控制台获取字符串
 * 2.将字符串转换成字符数组
 * 3.遍历数组
 * 4.初始化 TreeMap 用于保存每个字符和字符的个数
 * 5.将遍历的字符保存在 TreeMap 对象中
 * 6.拼接成一个 StringBuilder
 * 7.将StringBuilder 转换成字符串打印输出
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        //a a b c c d
        //1.从控制台获取字符串
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        //2.将字符串转换成字符数组
        char[] chars = str.toCharArray();
        //4.初始化 TreeMap 用于保存每个字符和字符的个数
        Map<Character,Integer> charCountMap = new TreeMap<>();
        //3.遍历数组
        for (int i = 0; i < chars.length; i++) {
            if(charCountMap.containsKey(chars[i])){
                charCountMap.put(chars[i],charCountMap.get(chars[i])+1);
            }else{
                //5.将遍历的字符保存在 TreeMap 对象中
                charCountMap.put(chars[i],1);
            }
        }
        //6.拼接成一个 StringBuilder
        StringBuilder builder = new StringBuilder();
        for (Character c : charCountMap.keySet()) {
            builder.append("key:"+c);
            builder.append(" value:"+charCountMap.get(c));
            builder.append("\n");
        }
        //7.将StringBuilder 转换成字符串打印输出
        System.out.println(builder.toString());
    }
}
